package com.rpc;

import com.rpc.client.NettyClient;
import com.rpc.demo.TestService;
import com.rpc.server.NettyServer;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StopWatch;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author yanzhuoleo
 * @version V1.0
 * @Description: main
 * @date 2018-01-16-下午6:49
 */

public class Main {
    public static void main(String[] args) throws Exception {
        new Main().initServer();
    }

    /**
     * 可以单独拆出来
     */
    private void initSpring() {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                "classpath:applicationContext.xml");
        applicationContext.start();
    }

    private void initServer() throws Exception {

        initSpring();
        NettyServer nettyServer = new NettyServer("127.0.0.1", 8899);
        NettyClient nettyClient = new NettyClient("127.0.0.1", 8899);
        Thread serverThread = new Thread(new Runnable() {
            @Override
            public void run() {
                nettyServer.init();
            }
        });

        Thread clientThread = new Thread(new Runnable() {
            @Override
            public void run() {
                nettyClient.init();
            }
        });

        serverThread.start();
        clientThread.start();

        //TODO:服务启动之后才可以进行rpc服务
        TimeUnit.SECONDS.sleep(1);

        TestService testService = nettyClient.create(TestService.class);
        StopWatch   stopWatch   = new StopWatch();

        for (int index = 0; index < 10; ++index) {
            stopWatch.start(String.valueOf(index));
            List msg = testService.getList(1);
            stopWatch.stop();
        }
        System.out.println(stopWatch.prettyPrint());
        nettyClient.close();
        nettyServer.close();
    }


}
